﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Data;

using DevExpress.XtraPrinting;
using DevExpress.XtraGrid;

namespace InfoGenerateSystem.Component
{
    /// <summary>
    /// DataSet生成表图类
    /// 
    /// 创建人:     王昊
    /// 创建时间:   2009-06-18
    /// 
    /// </summary>
    public class DataSetToImageExporter
    {
        //MemoryStream ms;
        PrintingSystem ps;
        PrintableComponentLink pcl;
        ImageExportOptions opts;
        frmGridToImage gridForm;
        GridControl grid;

        public DataSetToImageExporter()
        {
            ps = new PrintingSystem();
            pcl = new PrintableComponentLink(ps);
            opts = new ImageExportOptions();
            opts.Format = System.Drawing.Imaging.ImageFormat.Jpeg;

            gridForm = new frmGridToImage();
        }

        public void DoExportToImage(DataSet ds, Int32 imagePercent, String fileName)
        {
            //ms = new MemoryStream();
            grid = gridForm.BindDataAndGetGrid(ds);

            pcl.Component = grid;
            pcl.CreateDocument();
            //pcl.CustomPaperSize = new Size(1280, 800);
            //ps.Document.AutoFitToPagesWidth = 1280;
            //opts.PageRange = "1280";
            ps.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A3;
            ps.PageSettings.LeftMargin = 2;
            ps.PageSettings.RightMargin = 2;

            opts.Resolution = imagePercent;
            pcl.PrintingSystem.ExportToImage(fileName, opts);

            //Image img = Image.FromStream(ms);
            //return img;
        }

        //public void CloseImageStream() 
        //{
        //    ms.Close();
        //    ms.Dispose();
        //}

        public void DisposeGrid() 
        {
            //grid.Dispose();
        }
    }
}
